/**
 * 
 */
package Model;

import java.io.File;
import java.util.List;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Observable;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author enzo
 *
 */
public class ContactList extends Observable  {

	private List <contact> List;
	
	private boolean aSort,gSort,running;
	
	/**
	 * 
	 */
	public ContactList()
	{
		List=new ArrayList <contact>();
		running=true;
		aSort=false;
		gSort=false;
	}
	/**
	 * @param c
	 */
	public void addContact(contact c)
	{
		List.add(c);
		setChanged();
		notifyObservers(List);
	}
	/**
	 * @param c
	 */
	public void removeContact(contact c)
	{	// On retire le contact de la liste
		List.remove(c);
		// On supprime sa photo de la memoire
		if(c.getPhoto().contentEquals("Resources/"+c.getFirstname()+c.getName()+".png"))
		{
		File f = new File(c.getPhoto());
		f.delete();
		}
		setChanged();
		notifyObservers(List);
	}
	/**
	 * 
	 */
	public void alphaSort()
	{
		Thread t=new Thread(new Runnable(){
			
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			Collections.sort(List, new AlphaComparator());
			setChanged();
			notifyObservers(List);}
		});
		t.start();
	}
	/**
	 * 
	 */
	public void groupSort()
	{Thread t=new Thread(new Runnable(){
		
		
	@Override
	public void run() {
		// TODO Auto-generated method stub
		Collections.sort(List, new GroupComparator());
		setChanged();
		notifyObservers(List);}
	});
		t.start();
	}
	/**
	 * 
	 */
	public void saveAgenda()
	{
		File fichier=null;
		fichier=new File("Resources/Agenda.csv");
		FileWriter fw;
		try {
			fw = new FileWriter(fichier);
			//fw.write("Name;Firstname;Address;Company Address; Phone perso; Phone pro; email perso; email pro; URL; picture; Group  \n");
			for(contact c : List)
			{
			fw.write(c.toCsv());
			}
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 
	 */
	public void loadAgenda()
	{Thread t=new Thread(new Runnable(){
		
		
	@Override
	public void run() {
		// TODO Auto-generated method stub
		File fichier=new File("Resources/Agenda.csv");
		boolean firstLine = true;
		try {
			
			Scanner inputStream=new Scanner(fichier);
			while (inputStream.hasNext()) {
				// read single line
				String data = inputStream.nextLine();

			

				// Split line into several data strings
				String dataTab[] = data.split(";");
				contact c = new contact(dataTab[0],dataTab[1],dataTab[2],dataTab[3],dataTab[4],dataTab[5],dataTab[6],dataTab[7],dataTab[8],dataTab[9],dataTab[10]);
				List.add(c);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}});
	t.start();
	}
	/**
	 * @return the list
	 */
	public List<contact> getList() {
		return List;
	}


	/**
	 * @param aSort the aSort to set
	 */
	public void setaSort(boolean aSort) {
		this.aSort = aSort;
	}
	/**
	 * @param gSort the gSort to set
	 */
	public void setgSort(boolean gSort) {
		this.gSort = gSort;
	}

	/**
	 * @param running the running to set
	 */
	public void setRunning(boolean running) {
		this.running = running;
	}
	/*@Override
	public void run() {
		// TODO Auto-generated method stub
		loadAgenda();
		while (running)
		{
			if(aSort)
			{
				alphaSort();
				//System.out.println("top");
				aSort=false;
			}
			if(gSort)
			{
				groupSort();
				gSort=false;
			}
		}
		saveAgenda();
		
	}*/
}
